package com.yanqu.road.dao.impl.activity.businessreunion;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.businessreunion.BusinessReunionData;
import com.yanqu.road.entity.activity.businessreunion.RecallUserInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/4/19 18:37
 */
public class BusinessReunionDataDaoImpl extends TempDataObjectDao<BusinessReunionData> {
    private static String getSql = "select * from t_u_reunion_data where activityId = ?";

    public Map<Long, BusinessReunionData> getUserDataMap(int activityId) {
        Map<Long, BusinessReunionData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BusinessReunionData data = parse(rs);
                dataMap.put(data.getPlayerId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String insertSql = "insert into t_u_reunion_data(userId, activityId, recallCode, recallUserInfos, alreadyGetIntegral)" +
            "values(?,?,?,?,?)";
    @Override
    protected boolean addData(BusinessReunionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlayerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRecallCode()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRecallUserInfos())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAlreadyGetIntegral()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_reunion_data set recallCode = ?, recallUserInfos = ?, alreadyGetIntegral = ? where userId = ? and activityId = ?";
    @Override
    protected boolean updateData(BusinessReunionData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRecallCode()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRecallUserInfos())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAlreadyGetIntegral()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlayerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusinessReunionData parseData(ResultSet rs) throws SQLException {
        BusinessReunionData data = new BusinessReunionData();
        data.setPlayerId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setRecallCode(rs.getString("recallCode"));
        String value = rs.getString("recallUserInfos");
        if(!StringUtils.isNullOrEmpty(value)){
            List<RecallUserInfo> recallUserInfos = JSON.parseObject(value, new TypeReference<List<RecallUserInfo>>() {});
            data.setRecallUserInfos(recallUserInfos);
        }
        data.setAlreadyGetIntegral(rs.getInt("alreadyGetIntegral"));
        return data;
    }
}
